<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'nickname' => $this->nickname,
            'avatar' => $this->avatar(),
            'gender' => $this->gender,
            'status' => $this->status === 1,
            'role' => new RoleResource($this->roles->first()),
            'description' => $this->description,
            'can_update' => $request->user()->can('update', [$this->resource, 'users.update']),
            'can_delete' => $request->user()->can('update', [$this->resource, 'users.destroy']),
            'login_city' => $this->login_city,
            'login_time' => $this->login_time ? $this->login_time->diffForHumans() : '',
            'created_at' => $this->created_at->format('Y-m-d H:i:s'),
            'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
        ];
    }
}
